import _GENV from '@/common/env.js'
// 配置网站后台根目录
export const hostRoot = 'https://' + _GENV.Gdomain + '/public/upload/idcard/'


function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function (milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function (dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function (number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};


function createCode(length,type){/* 生成验证码的函数  length验证码长度  type 验证码类型 */
	let code="";
	let selectChar=new Array(1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	let numberChar = [1,2,3,4,5,6,7,8,9]
	for(let i=0;i<length;i++){
		if(type === 'number'){
			let charIndex=Math.floor(Math.random()*9);
			code += numberChar[charIndex];
		}else{
			let charIndex=Math.floor(Math.random()*61);
			code += selectChar[charIndex];
		}
	}
	return code;
}

/* 获取图片 */
function getImg(url){
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
		  src: url,
		  success(res) {
			 resolve(res.path)
		  }
		})
	})
}

/* 时间并格式化*/
// dataTimeFormat("yyyy-MM-dd HH:mm:ss")
// dataTimeFormat("yyyy-MM-dd")
// 第二个参数为时间参数，传入时间格式化返回
function dataTimeFormat(fmt, time) {
  const netime = new Date(time)
  const o = {
    'M+': netime.getMonth() + 1, // 月份
    'd+': netime.getDate(), // 日
    'H+': netime.getHours(), // 小时
    'm+': netime.getMinutes(), // 分
    's+': netime.getSeconds(), // 秒
    'q+': Math.floor((netime.getMonth() + 3) / 3), // 季度
    'S': netime.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (netime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (const k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
  return fmt
}

module.exports = {
	hostRoot,
	formatTime,
	formatLocation,
	dateUtils,
	createCode,
	dataTimeFormat,
	getImg
}
